import confirmTpl from './ngDialogConfirm.html';
import alertTpl from './ngDialogAlert.html';

@Inject('ngDialog', '$q', '$log')
class ngDialogService {

  confirm(content, data) {
    const deferred = this.$q.defer();

    this.ngDialog.openConfirm({
      template: confirmTpl,
      className: 'ngdialog-theme-default pa-confirm-theme confirm-dialog',
      controllerAs: 'ngDialogConfirm',
      controller($scope) {
        'ngInject';

        this.modalContent = content;
        this.confirm = () => {
          deferred.resolve(data);
          $scope.closeThisDialog();
        };
        this.closeDialog = () => {
          // deferred.resolve();
          deferred.reject();
          $scope.closeThisDialog();
        };
      },

    });
    return deferred.promise;
  }

  alertSuccess(content) {
    return this.ngDialog.openConfirm({
      template: alertTpl,
      className: 'ngdialog-theme-default pa-confirm-theme alert-dialog',
      controllerAs: 'vm',
      controller($log, $scope) {
        'ngInject';

        this.type = 1;
        this.modalContent = content;
        setTimeout(() => {
          $scope.closeThisDialog();
        }, 2000);
      },

    });
  }

  alertError(content) {
    return this.ngDialog.openConfirm({
      template: alertTpl,
      className: 'ngdialog-theme-default pa-confirm-theme alert-dialog',
      controllerAs: 'vm',
      controller($log, $scope) {
        'ngInject';

        this.type = 2;
        this.modalContent = content;
        setTimeout(() => {
          $scope.closeThisDialog();
        }, 2000);
      },

    });
  }

}

export default ngDialogService;
